package com.ln.asset.core.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ln.asset.common.result.R;
import com.ln.asset.core.pojo.Maintain;
import com.ln.asset.core.service.MaintainService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 维修表 前端控制器
 * </p>
 *
 * @author jijiaqi
 * @since 2022-05-02
 */
@Api(tags = "资产维修")
@RestController
@RequestMapping("/admin/core/maintain")
@Slf4j
public class MaintainController {
    @Resource
    private MaintainService maintainService;

    /**
     * 资产维修查询 +分页
     */
    @ApiOperation("获取资产维修分页列表")
    @GetMapping("/getMaintainPage/{page}/{limit}")
    public R getRecListPage( @ApiParam(value = "当前页码", required = true)
                             @PathVariable Long page,

                             @ApiParam(value = "每页记录数", required = true)
                             @PathVariable Long limit){
        Page<Maintain> objectPage = new Page<>(page,limit);
        IPage<Maintain> list = maintainService.getMainListPage(objectPage);
        return R.ok().data("list",list);
    }
    @ApiOperation("全查")
    @GetMapping("/list")
    public R list(){
        List<Maintain> list = maintainService.list(null);
        return R.ok().data("list",list);
    }
    /**
     * 增加资产维修
     */
    @ApiOperation("增加资产维修")
    @PostMapping("/insertMain")
    public R insertRec(@ApiParam(value = "资产维修记录", required = true)@RequestBody Maintain maintain){
        try {
            maintainService.insertMain(maintain);
            return R.ok().message("新增成功");
        }catch (Exception e){
            log.error(e.getMessage());
            return R.error().message("新增成功");
        }
    }
    /**
     * 修改资产维修
     */
    @ApiOperation("修改资产维修")
    @PostMapping("/updateMaintain/{status}")
    public R updateRecStatus(@ApiParam(value = "维修状态", required = true)@PathVariable Integer status,
                             HttpServletRequest request){
        try {
            Maintain maintain = (Maintain)request.getSession().getAttribute("maintain");
            maintainService.updateMainStatus(status,maintain);
            return R.ok().message("修改完成");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("删除维修信息")
    @DeleteMapping("/delMaintain/{id}")
    public R delMaintain(
            @ApiParam(value = "id",required = true) @PathVariable  Integer id){
        try {
            maintainService.delMain(id);
            return R.ok().message("删除成功");
        }catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

//    @ApiOperation("维修详情")
//    @GetMapping("/MaintainDetails/{id}")
//    public R transforDetails(@ApiParam(value = "id",required = true)
//                             @PathVariable Integer id){
//        Map<String,Object> map = maintainService.selectDetails(id);
//        return R.ok().data("transforDetails",map);
//    }

    //根据id查看
    //详情页面的接口
    @ApiOperation("维修详情")
    @GetMapping("/MaintainDetails/{id}")
    public R show(
            @ApiParam(value = "id", required = true)
            @PathVariable Integer id) {
        Maintain byId = maintainService.getById(id);
        return R.ok().data("m", byId);
    }
}

